<?php
include "connect/toolconnect.php";
include("self_form.php");
///////////////////////////////////////逻辑层/////////////////////////////////////////////////
////将数据新表登记在数据库
self_defined_form_register();
////创建数据表
creat_data();
//			////从数据表里查找数据  为提高效率暂时弃用
//			search_self_defined_form($yformname)
////做插入表单
creat_add_form();
////做更新表单
creat_eidt_form();
////插入和更新程序
creat_form_app();
////做表格
creat_grid();
////做展开行
crear_show_form();
////生成目录
make_contens();
////数据导出
self_export();
/////////////////////////////////////////函数层////////////////////////////////////////////////
////将数据新表登记在数据库
function  self_defined_form_register() {
	$content="";
	global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	$data_size=5;//参数的数量
	$zformname = $_POST['zformname'];
	$yformname = $_POST['yformname'];
	$sql_all_value="";
	for ($ii = 0; $ii < (sizeof($_POST) - 2) / $data_size; $ii++) {
		$zname[$ii] = $_POST['zname_' . $ii];
		$yname[$ii] = $_POST['yname_' . $ii];
		$char_type[$ii] = $_POST['char_type_' . $ii];
		$values[$ii] = $_POST['values_' . $ii];
		$sign[$ii] = $_POST['sign_' . $ii];
		$sql_all_value .="('" . $zformname . "', '" . $yformname . "','". $zname[$ii] . "', '" . $yname[$ii] . "', '" . $char_type[$ii] . "', '" . $values[$ii] . "', " . $sign[$ii] . '),';
	}
		$sql_all_value = substr($sql_all_value, 0, strlen($sql_all_value) - 1);
	//	判断数据表是否存在，以英文名为准。

		$sqlall = "INSERT INTO zj_self_defined_forms (`formszname`,`formsyname`,`fieldzname`,`fieldyname`,`char_type`,`values`,`sign`)values" . $sql_all_value;
//		print_r($sqlall);
		if (mysql_query($sqlall)) {
			echo( "表格登记成功");
		}

}
////创建数据表
function creat_data(){
	////创建数据表
	global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	$zname_sql = " `ID` int(50) NOT NULL AUTO_INCREMENT COMMENT '序号', "; //插入序号
	for ($ii = 0; $ii < sizeof($yname); $ii++) {
		switch($char_type[$ii]) {
			case 'radio':
			case 'checkbox':
			case 'select':
			case 'selectmul':
					$type='char';
					$size =255;
			break;
			case 'int':
				$type='int';
					$size =255;
			case 'char':
				$type='char';
					$size =255;
			continue;
			default:
			 		$type=$char_type[$ii];
					$size =$char_size[$ii];
			break;
		}
		if ($type == "datetime" || $type == "double"|| $type == "mediumtext") {
			$zname_sql .= "`" . $yname[$ii] . "` " . $type . " DEFAULT NULL  COMMENT '" . $zname[$ii] . "', ";        //`name` varchar(400) DEFAULT NULL  COMMENT '名称',
		} else {
			$zname_sql .= "`" . $yname[$ii] . "` " . $type . "(" . $size . ") DEFAULT NULL  COMMENT '" . $zname[$ii] . "', ";        //`name` varchar(400) DEFAULT NULL  COMMENT '名称',
		}

	}
	$zname_sql .= " `sub` tinyint(1)   DEFAULT NULL  COMMENT '提交',";
	$zname_sql .= " PRIMARY KEY (`ID`) ";
	//	$zname_sql=substr($zname_sql,0,strlen($zname_sql)-2);
	$sql = "CREATE TABLE IF NOT EXISTS self_" . $yformname . "(" . $zname_sql . ")ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin  ;";
	echo("<br>creeatdata:".$sql);
	if (mysql_query($sql)) {
		echo( "表格创建成功");
	}
}



////做插入表单
function creat_add_form(){
	//创建表单
	global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	$form_item="";
	$content="";
		$text ="<?php \n";
		$text .="\$url=\"self_" . $yformname."\"; \n";
		$text .="require_once \"../connect/toolconnect.php\"; \n";
		$text .="\$rs = mysql_query(\"select * from \".\$url ); \n";
		//总记录数
		$text .="\$newnums = mysql_num_rows(\$rs)+1; \n";
		$text .="?> \n";
		$text .="<script type=\"text/javascript\" > \n";
		$text .="var furl= \"#<?php echo \$url; ?>\" \n";
		$text .="</script> \n";

	$form_add = new form("self_" . $yformname . "_app.php?ctr=add");     //提交到
	$form_add->selftext=$text;
	$form_add->layout = true;                                     //不使用表格布局，大家可以把这句注释掉看结果有何不同
	for ($ii = 0; $ii < sizeof($yname); $ii++) {
	    $form_item[$ii] = $form_add->form_easy($yname[$ii], $zname[$ii], $char_type[$ii],$values[$ii]);
	}
	$form_item[$ii] = $form_add->form_easy("submit","提交","submit");
	array_unshift($form_item, $form_add->form_easy('ID','序号', 'char','$newnums'));//####################要改 不能编辑
	//print_r($form_item);
	//显示表单
	$content = $form_add->create_form($form_item);
	//输出php
	$file_path = "./self_creat_form/self_" . $yformname . "_add.php";
	file_put_contents($file_path, $content);
}
////做更新表单
function creat_eidt_form(){
	//创建表单
		global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	$form_item="";
	$content="";
	$text="<?php \n";
	$text .="\$url=\"self_" . $yformname."\"; \n";
	$text.="require_once \"../connect/toolconnect.php\";  \n";
	$text.="\$id = intval(trim(\$_GET['ID'])); \n";
	$text.="\$sql = \"select * from {\$url} where ID= {\$id}\"; \n";
	$text.="\$rs = mysql_query(\$sql); \n";
	$text.="\$row = mysql_fetch_assoc(\$rs); \n  ?>\n";
	$text .="<script type=\"text/javascript\" > \n";
	$text .="var furl= \"#<?php echo \$url; ?>\" \n";
	$text .="</script> \n";
	$form_edit = new form("self_" . $yformname . "_app.php?ctr=edit");     //提交到
	$form_edit->selftext = $text;
	$form_edit->layout = true;                                     //不使用表格布局，大家可以把这句注释掉看结果有何不同
	for ($ii = 0; $ii < sizeof($yname); $ii++) {
		switch($char_type[$ii]) {
			case 'radio':
				$form_item[$ii] = $form_edit->form_easy($yname[$ii], $zname[$ii],$char_type[$ii],$values[$ii]);
				//只能在最后加一段js  php不能实现 才能实现  定义变量
				$js_content.="<script>\n";
				$js_content.="<?php echo (\"var " . $yname[$ii] . "=\\\"\".\$row['" . $yname[$ii] . "'].\"\\\"\");?>;\n";
				$js_content.="\$(\"input[ value='\"+" . $yname[$ii] . "+\"'][ name='" . $yname[$ii] . "']\").attr(\"checked\",\"checked\");\n</script>";
			continue;
			case 'checkbox':
				$form_item[$ii] = $form_edit->form_easy($yname[$ii], $zname[$ii],$char_type[$ii],$values[$ii]);
				//只能在最后加一段js  php不能实现 才能实现  定义变量
				$js_content.="<script>\n";
				$js_content.="<?php echo (\"var " . $yname[$ii] . "=\\\"\".\$row['" . $yname[$ii] . "'].\"\\\"\");?>;\n";
				$js_content.=$yname[$ii] . "=" . $yname[$ii] . ".split(';;');\n";
				$js_content.="for (i=0;i<" . $yname[$ii] . ".length ;i++ ){\n";
				$js_content.="\$(\"input[ value='\"+" . $yname[$ii] . "[i]+\"'][ name='" . $yname[$ii] . "[]']\").attr(\"checked\",\"checked\");}\n</script>";
			continue;
			case 'select':
				$form_item[$ii] = $form_edit->form_easy($yname[$ii], $zname[$ii],$char_type[$ii],$values[$ii]);
				//只能在最后加一段js  php不能实现 才能实现  定义变量
				$js_content.="<script>\n";
				$js_content.="<?php echo (\"var " . $yname[$ii] . "=\\\"\".\$row['" . $yname[$ii] . "'].\"\\\"\");?>;\n";

				$js_content.="\$(\"#" . $yname[$ii] . "\").val(" . $yname[$ii] .");\n</script>";
				continue;
			case 'selectmul':
				$form_item[$ii] = $form_edit->form_easy($yname[$ii], $zname[$ii],$char_type[$ii],$values[$ii]);
				//只能在最后加一段js  php不能实现 才能实现  定义变量
				$js_content.="<script>\n";
				$js_content.="<?php echo (\"var " . $yname[$ii] . "=\\\"\".\$row['" . $yname[$ii] . "'].\"\\\"\");?>;\n";
				$js_content.=$yname[$ii] . "=" . $yname[$ii] . ".split(';;');\n";
				$js_content.="\$(\"#" . $yname[$ii] . "\").val(" . $yname[$ii] .");\n</script>";
			continue;
			default:
				$form_item[$ii] = $form_edit->form_easy($yname[$ii], $zname[$ii],$char_type[$ii],"\$row['".$yname[$ii]."']");
			continue;

		}
	}
	$form_item[$ii] = $form_edit->form_easy("submit","提交","submit");
	array_unshift($form_item, $form_edit->form_easy('ID','序号', 'char','$id'));//####################要改 不能编辑
	//print_r($form_item);
	//显示表单
	$content = $form_edit->create_form($form_item);
	$content.=$js_content;
	//输出php
	$file_path = "./self_creat_form/self_" . $yformname . "_edit.php";
	file_put_contents($file_path, $content);
}
////插入和更新程序
function creat_form_app(){
	$acept_str = "<?php \n require_once \"../connect/toolconnect.php\"; \n session_start(); \n if(empty(\$_SESSION['user'])) { \n echo(\"<script>alert('归还信息填写不完整！'); </script>\"); \n	header(\"Location: ../login.php\"); \n } \n";
		global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	$content="";
	$sql_add1 = "INSERT INTO self_" . $yformname . " ( ";
	$sql_add2 = ")values(";
	$sql_edit="\"update self_" . $yformname . " set ";
	$acept_str .= "\$id=trim(\$_POST['ID']);\n";
	$acept_str .= "\$ctr=trim(\$_GET['ctr']);\n";
	for ($ii = 0; $ii < sizeof($yname); $ii++) {
		//创建接受表单
		switch($char_type[$ii]) {//自动循环 将多选租和后重新放在一个变量里面
			case 'checkbox':
			case 'selectmul':
				$acept_str .= "\$" . $yname[$ii] . "=trim(\$_POST['" . $yname[$ii] . "']);\n";
				$acept_str .= "\$temp_" . $yname[$ii] . "=\"\";\n";
				$acept_str .= "for(\$ii=0; \$ii<sizeof(\$_POST['" . $yname[$ii] . "']); \$ii++) {\n";
				$acept_str .= "\t\$temp_" . $yname[$ii] . ".=\$_POST['" . $yname[$ii] . "'][\$ii].\";;\";\n}\n";
				$sql_add1 .= "`" . $yname[$ii] . "`, ";
				$sql_add2 .= "'{\$temp_" . $yname[$ii] . "}', ";
				$sql_edit.=  $yname[$ii] . "='{\$temp_" . $yname[$ii] . "}', ";
			continue;
			default:
				$acept_str .= "\$" . $yname[$ii] . "=trim(\$_POST['" . $yname[$ii] . "']);\n";
				$sql_add1 .= "`" . $yname[$ii] . "`, ";
				$sql_add2 .= "'{\$" . $yname[$ii] . "}', ";
				$sql_edit.=  $yname[$ii] . "='{\$" . $yname[$ii] . "}', ";
			continue;

		}
	}
	$sql_edit = substr($sql_edit, 0, strlen($sql_edit) - 2);
	$sql_edit.=" where ID = {\$id}\"";
	$sql_add1 = substr($sql_add1, 0, strlen($sql_add1) - 2);
	$sql_add2 = substr($sql_add2, 0, strlen($sql_add2) - 2);
	$sql_add2 .= ")";
	$sql_add3 = $sql_add1 . $sql_add2;
	$acept_str .= "\$sql_edit=" . $sql_edit . ";\n";
	$acept_str .= "\$sql_add3=\"" . $sql_add3 . "\";\n";
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	$acept_str.="\$sql = \"select * from self_" . $yformname . " where ID= {\$id}\"; \n";
	$acept_str.="\$rs = mysql_query(\$sql); \n";
	$acept_str.="\$row = mysql_fetch_assoc(\$rs); \n  \n";
	//$acept_str .=" echo \$sql_edit.\" esqla\".\$sql_add .\" row=\".\$row;\n";//#######################################调试使用 打印参数
	$acept_str.="if(\$ctr=='add'){\n ";
	$acept_str.="	if(\$row['ID']==\$id){\n";//######################################问题严重
	$acept_str.="	echo \"<script>alert('请勿重复添加！');</script>\";\n	}else{\n";
	$acept_str.="		if(mysql_query(\$sql_add3)) {echo \"<script>alert('添加成功！');</script>\";}else{echo \"<script>alert('添加失败！');</script>\".\$sql_add3;}\n";
	$acept_str.="	}\n";
	$acept_str.="}elseif(\$ctr=='edit'){\n";
	$acept_str.="		if(mysql_query(\$sql_edit)) {echo \"<script>alert('更新成功！');window.history.back();</script>\";}\n";
//	$acept_str.="		if(mysql_query(\$sql_edit)) {echo \"<script>alert('更新成功！');window.location.href = './self_" . $yformname . "_grid.php';</script>\";}\n";
	$acept_str.=" }\n?>";
//	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	//输出php
	$file_path = "./self_creat_form/self_" . $yformname . "_app.php";
	file_put_contents($file_path, $acept_str);
}
////做表格
function  creat_grid(){
	global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
$grid_str = <<<EOF
<head>
	<meta charset="UTF-8">
	<meta http-equiv="cache-control" content="no-cache">
	<title> some_grid</title>
	<link rel="stylesheet" type="text/css" href="../css/easyui_themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../css/easyui_themes/icon.css">
	<script type="text/javascript" src="../js/jquery-1.9.1.js"></script>
	<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../js/datagrid-filter.js"></script>
	<script type="text/javascript" src="../js/datagrid-detailview.js"></script>
	<script src="../canvas/index_data/jq-signature.js"></script>
</head>
	<table id="dg" title=""  data-options="url:'../some_json.php?class1=self_{$yformname}',toolbar:'#toolbar',pagination:true,singleSelect:true,nowrap:false ">
		<thead>
			<tr>
			<th data-options="field:'ID', sortable:true ,width:50 ">序号</th>
EOF;
$grid_str_end = <<<EOF
			</tr>
		</thead>
	</table>
	<div id="toolbar" style="height:auto">

		<a href="self_{$yformname}_export.php?class1=self_{$yformname}" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" >导出</a>
	</div>
<div id="signdlg" class="easyui-dialog" title="请签名" data-options="iconCls:'icon-save'" style="width:650px;height:350px;padding:10px">
            <span>ID：</span><span id="signdlgid"></span><span> &nbsp signer: </span><span id="signdlgsigner"></span>&nbsp row: </span><span id="signdlgrow"></span>
	        <div class="js-signature"  data-height="200" data-width="600" data-border="2px solid black" data-line-color="#330000" >		</div>
	        <p><button id="clearBtn" class="btn btn-default" onclick="clearCanvas();">清除</button>&nbsp;&nbsp;&nbsp;
            <button id="saveBtn" class="btn btn-default" onclick="saveSignature('self_{$yformname}');" disabled="disabled">保存</button>&nbsp;&nbsp;&nbsp;
            <!--工具栏 	 -->
</div>
<script type="text/javascript">
    \$(function () {
        //签名
        if (\$('.js-signature').length) {
            \$('.js-signature').jqSignature();
        }
        //对话窗口
        \$('#signdlg').window('close');
			\$('#dg').datagrid({
				fit:true,
				autoRowHeight:true,
				view: detailview,
				detailFormatter:function(index,row){
					return '<div class="ddv"></div>';
				},
				onExpandRow: function(index,row){
					var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
					ddv.panel({
						border:false,
						cache:true,
						href:'./self_{$yformname}_show_form.php?ID='+row.ID+'&index='+index+'&class1=self_{$yformname}',
						onLoad:function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
							$('#dg').datagrid('selectRow',index);
							$('#dg').datagrid('getRowDetail',index).find('form').form('load',row);
						}
					});
					$('#dg').datagrid('fixDetailRowHeight',index);
				}
        });
        //筛选 现在不可以远程
        //$('#dg').datagrid('enableFilter');
    })
//签名对话框
function signapp(ID,signer,index){
	\$('#signdlgid').html(ID);
	\$('#signdlgsigner').html(signer);
	\$('#signdlgrow').html(index+1);
	\$('#signdlg').window('open');
	clearCanvas();
}
//清除签名
function clearCanvas() {
	\$('.js-signature').eq(0).jqSignature('clearCanvas');
	\$('#saveBtn').attr('disabled', true);
}
//保存签名
function saveSignature(class1){
	var dataUrl = \$('.js-signature').eq(0).jqSignature('getDataURL');
	var img = \$('<img>').attr('src', dataUrl);
	\$.post("../canvas/save.php",{data:dataUrl,class1:class1,ID:\$('#signdlgid').html(),person:\$('#signdlgsigner').html() },function(data) {
		//弹出签名
		//alert(data);
		//保存按钮显示
			$('#saveBtn').attr('disabled', true);
			$('#signdlg').window('close');
			$('#dg').datagrid('collapseRow',$('#signdlgrow').html()-1);//关闭打开行相当于刷新
			$('#dg').datagrid('expandRow',$('#signdlgrow').html()-1);
		}
	)
}

//清除保存按钮功能
$('.js-signature').eq(0).on('jq.signature.changed', function() {
		$('#saveBtn').attr('disabled', false);
	});
//归还
function returnapp(ID,class1){
window.location.href='self_{$yformname}_edit.php?ID='+ID+'&class1='+class1;
}
	</script>
EOF;
	for ($ii = 0; $ii < sizeof($yname); $ii++) {
		//生成浏览表
		$grid_str .= "<th data-options=\"field:'{$yname[$ii]}', sortable:true \">{$zname[$ii]}</th> \n ";
		$sql_all_value .="('" . $zformname . "', '" . $yformname . "','". $zname[$ii] . "', '" . $yname[$ii] . "', '" . $char_type[$ii] . "', " . $char_size[$ii] . ", " . $show_width[$ii] . ", " .  $sign[$ii] . '),';
	}
	////生成浏览表格
	$file_path = "./self_creat_form/self_" . $yformname . "_grid.php";
	$grid_str.=$grid_str_end;
	file_put_contents($file_path, $grid_str);
	//	echo("<script>alert('操作成功！');</script>");

}
////做展开行
function crear_show_form(){
		global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	$content="<meta charset=\"UTF-8\"> \n";
	$content.="<?php \n";
	$content.="\$ID=\$_REQUEST['ID']; \n";
	$content.="\$index=\$_REQUEST['index']; \n";
	$content.="\$class1=\$_REQUEST['class1']; \n";
	$content.=" \n";
	//看是否需要签名
	for ($ii = 0; $ii < sizeof($yname); $ii++) {
		if($sign[$ii]==1) {
			$content.="\$img".$ii." = '../canvas/sign/'.\$class1.\"/\".md5(\$ID.'{$yname[$ii]}').'.png'; \n";
//			$content.=" echo \"<span>{$zname[$ii]}</span><img class='signimg'  src=\".\$borrower_img.\"  alt='请签名' />\"; \n";
			$content.="  \n";
			$content.="if (file_exists(\$img".$ii.")) {\n";
			$content.="	echo \"<span>{$zname[$ii]}签名：</span><img class='signimg'  src='\$img".$ii." ' alt='请签名' />\";\n";
			$content.="} else {\n";
			$content.="	echo \"<span>{$zname[$ii]}签名：</span><img class='unsignimg' src='#' alt='请签名'   onclick='signapp(\".\$ID.\",\\\"{$yname[$ii]}\\\",\".\$index.\")' />\";\n";
			$content.="}\n";
		}
	}
	$content.="	//是否需要显示归还按钮 \n";
	$content.="require_once \"../connect/toolconnect.php\"; \n";
	$content.="\$id = intval(trim(\$_GET['ID'])); \n";
	$content.="\$sql = \"select * from \".\$class1.\" where ID= {\$id}\"; \n";
	$content.="\$rs = mysql_query(\$sql); \n";
	$content.="\$row = mysql_fetch_assoc(\$rs); \n";
	$content.="if(\$row['sub']!=1) { \n";
	$content.="	echo 	\"<a class='easyui-linkbutton' data-options=\\\"iconCls:'icon-edit'\\\" onclick=\\\"returnapp(\".\$ID.\",'\".\$class1.\"')\\\">编辑</a>\";//不跳转直接改成连接 \n";
	$content.="} \n";
	$content.="	echo(\"<br>\"); \n";
//后面防样式
	$content.="?> \n<style>\n";
	$content.=".signimg:active{\n";
	$content.="	cursor: -webkit-zoom-in;\n";
	$content.="	height:90px;\n";
	$content.="	width:270px;\n";
	$content.="}\n";
	$content.=".signimg{\n";
	$content.="	cursor: -webkit-zoom-in;\n";
	$content.="height:30px;\n";
	$content.="width:90px;\n";
	$content.="}\n";
	$content.=".unsignimg{\n";
	$content.="	cursor:pointer;\n";
	$content.="	height:30px;\n";
	$content.="width:90px;\n";
	$content.="}\n";
	$content.=".evidenceimg{\n";
	$content.="	height:50px;\n";
	$content.="	width:50px;\n";
	$content.="}\n";
	$content.=".evidenceimg:active{\n";
	$content.="	height:300px;\n";
	$content.="	width:300px;\n";
	$content.="}\n";
	$content.="</style>\n";
	$file_path = "./self_creat_form/self_" . $yformname . "_show_form.php";
	file_put_contents($file_path, $content);
}
////生成目录
function make_contens() {
		global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	//读取
	$jsonaddr = "tree_data2.json";
	$json = (json_decode(file_get_contents($jsonaddr), true));
	$arr['text'] = $zformname;
	$arr['children'][0]['text'] = $zformname . "增加";
	$arr['children'][0]['url'] = "self_creat_form/self_" . $yformname . "_add.php";
	$arr['children'][1]['text'] = $zformname . "统计";
	$arr['children'][1]['url'] = "self_creat_form/self_" . $yformname . "_grid.php";
	array_push($json, $arr);
	file_put_contents($jsonaddr, json_encode($json));
}
////数据导出
function self_export() {
	global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;

$content="\n";
$content.="<?php\n";
$content.="include \"../connect/toolconnect.php\";\n";
$content.="\$rs = mysql_query(\"select * from \".\$_GET['class1'] );\n";
$content.="\$row = mysql_fetch_row(\$rs);\n";
$temp="";
for($ii=0; $ii<sizeof($zname); $ii++) {
	$temp.=$zname[$ii].",";
}
$temp=substr($temp, 0, strlen($temp) - 1)."";
$content.="\$content=\"".$temp."\\"."n\";\n";
$content.="	while(\$row = mysql_fetch_assoc(\$rs)) {\n";
$temp="";
for($ii=0; $ii<sizeof($yname); $ii++) {
	$temp.="\$row['".$yname[$ii]."'].','.";
}
$temp=substr($temp, 0, strlen($temp) - 5);
$content.="	\$content.=".$temp.".\"\\"."n\";\n";
$content.="	}\n";
$content.="	\$file_sub_path=\$_SERVER['DOCUMENT_ROOT'].\"/zjgrid/down/\";\n";
$content.="	\$file_name=date().\$_GET['class1'].\".csv\";\n";
$content.="	\$file_path=\$file_sub_path.\$file_name;\n";
$content.="file_put_contents(\$file_path, \$content);\n";
$content.="//文件导出\n";
$content.="	//用以解决中文不能显示出来的问题\n";
$content.="	\$file_name=iconv(\"utf-8\",\"gb2312\",\$file_name);\n";
$content.="	//首先要判断给定的文件存在与否\n";
$content.="	if(!file_exists(\$file_path)){\n";
$content.="	echo \"没有该文件文件\";\n";
$content.="	return ;\n";
$content.="	}\n";
$content.="	\$fp=fopen(\$file_path,\"r\");\n";
$content.="	\$file_size=filesize(\$file_path);\n";
$content.="	//下载文件需要用到的头\n";
$content.="	Header(\"Content-type: application/octet-stream\");\n";
$content.="	Header(\"Accept-Ranges: bytes\");\n";
$content.="	Header(\"Accept-Length:\".\$file_size);\n";
$content.="	Header(\"Content-Disposition: attachment; filename=\".\$file_name);\n";
$content.="	\$buffer=1024;\n";
$content.="	\$file_count=0;\n";
$content.="	//向浏览器返回数据\n";
$content.="	while(!feof(\$fp) && \$file_count<\$file_size){\n";
$content.="	\$file_con=fread(\$fp,\$buffer);\n";
$content.="	\$file_count+=\$buffer;\n";
$content.="	echo \$file_con;\n";
$content.="	}\n";
$content.="	fclose(\$fp);\n";
$content.="?>\n";
	$file_path = "./self_creat_form/self_" . $yformname . "_export.php";
	file_put_contents($file_path, $content);
}
////从数据表里查找数据
function search_self_defined_form($yformname) {
		global  $zformname,$yformname,$zname,$yname,$char_type,$char_size,$show_width,$show_size,$values,$document,$sign,$data_size;
	$sql = "select * from ".$yformname;
	$rs = mysql_query($sql);
	$row = mysql_fetch_assoc($rs);
	for ($ii = 0; $ii < sizeof($yname); $ii++) {
    $zname[$ii] = $row['zname_' . $ii];
    $yname[$ii] = $row['yname_' . $ii];
    $char_type[$ii] = $row['char_type_' . $ii];
    $char_size[$ii] = $row['char_size_' . $ii];
    $show_width[$ii] = $row['show_width_' . $ii];
    $sign[$ii] = $row['sign_' . $ii];
	}
}